﻿@using LanDBom.Resources
@using LanDBom.Resources
@{
    ViewBag.Title = "Requisition_form";
}
@section styles{
    <style>
        input {
            width: 100%;
        }

        fieldset div {
            margin-bottom: 5px
        }

        #finddiv1 {
            width: 100%;
        }

        select {
            width: 100%;
        }

        #viewdiv {
            width: 100%;
            overflow: scroll;
            height: 500px;
        }
    </style>
}
<fieldset class="boun">
    <legend></legend>
    <div class="row bian">
        <label class="col-sm-3 breadcrumb-item ">步骤一：申请单创建</label>
        <ul id="lisul" class="row col-sm-5">
            <li class="lisu">1</li>&rArr;
            <li class="lisu"></li>&rArr;
            <li class="lisu"></li>&rArr;
            <li class="lisu"></li>&rArr;
            <li class="lisu"></li>&rArr;
            <li class="lisu"></li>&rArr;
            <li class="lisu"></li>
        </ul>
    </div>
</fieldset>
<h4>@Resource.String1272</h4>
<from>
    <div id="finddiv1">
        <fieldset class="boun">
            <legend class="len">@Resource.String1235</legend>
            <div class="row">
                <div class="col-1">@Resource.String1282</div>
                <div class="col-2"><input type="text" id="BB_SQ_xmmc" /></div>
                <div class="col-1">@Resource.String1283</div>
                <div class="col-2"><input type="text" id="BB_SQ_khmc" /></div>
                <div class="col-1">@Resource.String402</div>
                <div class="col-2"><input type="date" id="BB_SQ_sqrq" /></div>
                <div class="col-2 btn-sm  btn-warning" id="BB_WT_cx" style="text-align:center">@Resource.String1235</div>
            </div>
        </fieldset>
    </div>
    <button class="btn" id="FQxbj" style="margin-top:10px;">@Resource.String1285</button>
    <div id="viewdiv">
        <table class="table table-sm gun" id="btable">
            <thead class="thead-light">
                <tr>
                    <th scope="col">序号</th>
                    <th scope="col">@Resource.String152</th>
                    <th scope="col">@Resource.String153</th>
                    <th scope="col">@Resource.String154</th>
                    <th scope="col">@Resource.String160</th>
                    <th scope="col">@Resource.String385</th>
                </tr>
            </thead>
            <tbody data-spy="scroll" id="BB_WT_tb">
            </tbody>
        </table>
    </div>
    <div id="WT_dh_mb2" style="display: none; position: absolute; border: 1px black solid; left: 25%; top: 33%; background-color: white; background-color: #f1f1f1;width:400px;height:150px">
        <div style="text-align: right; background-color: #84acde;"><span class="WT_qxbh2" style=" margin: 20px;"><i class="bi bi-x-square"></i></span></div>
        <div id="WJ_ID" style="display:none"></div>
        <div class="row">
            <div class="col-4">@Resource.String201：</div>
            <div id="WT_dhr2" class="col-8"></div>
        </div>
        <p></p>
        <div class="row">
            <div class="col-4">@Resource.String202</div>
            <div class="col-8"><textarea class="col-12" id="WT_dhly2"></textarea></div>
        </div>
        <p></p>
        <div style="text-align:center">
            <button class="WT_qxbh2">@Resource.String203</button>
        </div>
    </div>
</from>

@section scripts
{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(function () {

            get_contracts()
            Generate_report()
            $("#li1").css({ "color": "black", "background-color": "white" })
            $(document).on("click", "#BB_WT_cx", function () {
                get_contracts()
            })
            $(document).on("click", "#app_jux", function (e) {
                var id = $(e.target).parent().children().eq(1).text()

                window.location.href = "/Application/Application?id=" + id;
            })
            $(document).on("click", "#FQxbj", function (e) {
                console.log($("#Login_bm").text())
                if ($("#Login_bm").text() == "业务" || $("#Login_zj").text() > 2) {
                    window.location.href = "/Application/Application"
                }
                else {
                    alert("权限不足")
                }
            })
            $(document).on("click", "#app_sc", function (e) {
                var id = $(e.target).parent().children().eq(2).text()
                $.ajax({
                    url: '/Application/Requisition_form',
                    type: 'GET',
                    data: { "state": 2, "BB_SQ_xmmc": id },
                    success: function (wsde) { 
                        location.reload(true)
                        alert(wsde)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            })
            //搜索申请单信息
            function get_contracts() {
                var BB_SQ_xmmc = $("#BB_SQ_xmmc").val()
                var BB_SQ_khmc = $("#BB_SQ_khmc").val()
                var BB_SQ_sqrq = $("#BB_SQ_sqrq").val()
                var BB_SQ_gjz = $("#BB_SQ_gjz").val()
                $.ajax({
                    url: '/Application/Requisition_form',
                    type: 'GET',
                    data: { "BB_SQ_xmmc": BB_SQ_xmmc, "BB_SQ_khmc": BB_SQ_khmc, "BB_SQ_sqrq": BB_SQ_sqrq, "BB_SQ_gjz": BB_SQ_gjz, "state": 1 },
                    success: function (wt) {
                        console.log(wt)
                        newWT(wt)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            function find_liy(id) {
                $.ajax({
                    url: '/Application/Requisition_form',
                    type: 'GET',
                    data: { "id": id, "state": 3 },
                    success: function (zc) {
                        $.each(zc, function (i, obj) {
                            $("#WT_dhly2").val(obj.APP_liy)
                            $("#WT_dhr2").text(obj.APP_shr)
                        })
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            //点击关闭驳回面板
            $(document).on("click", ".WT_qxbh2", function (e) {
                $("#WT_dh_mb2").css({ "display": "none" })
                $("#WT_dhly2").val("")
                $("#WT_dhr2").text("")
            })
            //点击查看的打回理由
            $(document).on("click", "#find_yuany", function (e) {
                id = $(e.target).parent().parent().children().eq(1).text()
                console.log(id)
                find_liy(id)
                $("#WT_dh_mb2").css({ "display": "block" })
            })

            function newWT(wt) {
                $('#BB_WT_tb tr').remove();
                var number = 1
                var number1 = 1
                $.each(wt, function (i, item) {
                    console.log(item.App_zhuang)
                    if (item.App_zhuang == "驳回") {
                        var $tr =
                            $(`
                                <tr>
                                    <th scope="col" >${number1}</th>
                                    <th scope="col" style="display:none">${item.App_id}</th>
                                    <th scope="col" id="WT_SJ_S1">${item.AP_bian}</th>
                                    <th scope="col" id="WT_SJ_S2">${item.App_Kehu}</th>
                                    <th scope="col" id="WT_SJ_S3">${item.APP_name}</th>
                                    <th scope="col" id="WT_SJ_S4">${item.App_qishi}</th>
                                    <th scope="col" id="WT_SJ_S5">${item.App_JTime}</th>
                                    <th scope="col" id="WT_SJ_S5"><a href="#" id="find_yuany">查看驳回理由</a></th>
                                    <th scope="col" id="app_jux" style="color:#2e00ff">重新编辑</th>
                                    <th scope="col" id="app_sc" style="color:red">删除</th>
                                </tr>
                            `);
                        
                        if ($("#Login_sf").text() == "客户经理" || $("#Login_sf").text() == "销售助理") {
                            $("#BB_WT_tb").append($tr);
                            number1++
                        }
                        else if ($("#Login_sf").text() == "销售") {
                            if (item.AP_ren == $("#Login_N").text()) {
                                $("#BB_WT_tb").append($tr);
                                number1++
                            }
                        }
                    }
                    if (item.App_bei == "保存") {
                        var $tr =
                            $(`
                                <tr>
                                    <th scope="col" >${number}</th>
                                    <th scope="col" style="display:none">${item.App_id}</th>
                                    <th scope="col" id="WT_SJ_S1">${item.AP_bian}</th>
                                    <th scope="col" id="WT_SJ_S2">${item.App_Kehu}</th>
                                    <th scope="col" id="WT_SJ_S3">${item.APP_name}</th>
                                    <th scope="col" id="WT_SJ_S4">${item.App_qishi}</th>
                                    <th scope="col" id="WT_SJ_S5">${item.App_JTime}</th>
                                    <th scope="col" id="WT_SJ_S5">保存</th>
                                    <th scope="col" id="app_jux" style="color:#2e00ff">继续</th>
                                    <th scope="col" id="app_sc" style="color:red">删除</th>
                                </tr>
                            `);
                        
                        if ($("#Login_sf").text() == "客户经理" || $("#Login_sf").text() == "销售助理") {
                            $("#BB_WT_tb").append($tr);
                            number++
                        }
                        else if ($("#Login_sf").text() == "销售") {
                            if (item.AP_ren == $("#Login_N").text()) {
                                $("#BB_WT_tb").append($tr);
                                number++
                            }
                        }
                    }
                })
            }
            //下载excel
            function Generate_report() {
                $(document).on("click", "#BB_WT_scbb", function () {
                    var CG_XX = [];
                    var WT_RZ = [];
                    var check = $("input[type=checkbox]:checked").parent().parent().length;
                    if (check < 0) {
                        alert(0);
                    }
                    else {
                        $("input[type=checkbox]:checked").parent().parent().each(function (index, obj) {
                            CG_XX.push({
                                AP_bian: $("th[id='WT_SJ_S1']", obj).text(),
                                App_Kehu: $("div[id='WT_SJ_S2']", obj).text(),
                                APP_name: $("th[id='WT_SJ_S3']", obj).text(),
                                App_qishi: $("th[id='WT_SJ_S4']", obj).text(),
                                App_JTime: $("th[id='WT_SJ_S5']", obj).text(),
                            })
                            console.log("共" + CG_XX.length + "数据");
                            console.log(CG_XX);
                        })
                        var Employee_path = $("#Login_path").text()
                        console.log(Employee_path)

                        var userInput = prompt("请输入文件名：", "");
                        if (userInput != null) {
                            $.ajax({
                                url: "/Application/DownloadExcel",
                                type: 'POST',
                                data: { "Designdraws": CG_XX, "WT_CD": CG_XX.length, "state": 1, "excelName": userInput, "Employee_path": Employee_path },
                                success: function (text) {
                                    alert("下载成功")
                                },
                                error: function (text) {
                                    alert("系统错误");
                                }
                            })
                        }

                    }
                })
            }
        })
    </script>
}